/** History 
 * 
 *  2013-01-11 建置Library 可以存取根目錄的image
 */
package com.example.sample_for_work_library;

import java.io.File;
import java.io.FilenameFilter;


import android.os.Environment;
import android.util.Log;
/**
 * @author 說明:
 * Android 手機資料夾存取 image Library  <br/>
 * <br/>
 * 使用方法: Dir_file.img_file();
 */
public class Dir_file {

		public static CharSequence[] img_file() {
			File[] dir_file = check_dir("",imageFilter());
			File[] dir_file2 = check_dir("");

			// 將目錄內容建立清單
			CharSequence[] list = new CharSequence[dir_file.length];
			for (int i = 0; i < dir_file2.length ; i++){
				if (dir_file2[i].isDirectory()){
					check_dir("/"+dir_file2[i].getName(),imageFilter());
				}
			}
			for (int i = 0; i < list.length; i++) {
				list[i] = dir_file[i].getName();	
			}
			return list;
		}
		
		//圖片過濾器
		public static FilenameFilter imageFilter(){
			FilenameFilter img_filter = new FilenameFilter() {
				private String[] filter = {"jpg","png","jpeg","bmp","gif","tiff"};
				@Override
				public boolean accept(File dir, String filename) {

					for (int i = 0; i < filter.length; i++) {

						if (filename.indexOf(filter[i]) != -1)
							return true;
					}
					return false;
				}
			};
			
			return img_filter;
		}
		
		//顯示檔名
		public static void read_file_content(File[] file){
			if (file!=null){
			CharSequence[] list = new CharSequence[file.length];
			for(int i=0;i<list.length;i++){
				list[i] = file[i].getName();
				Log.i("name"+file.toString(), list[i].toString());
			}
			}
		}
		
		
		//讀取路徑
		public static File[] check_dir(String dir_root){
			File DirPath = new File(Environment.getExternalStorageDirectory()+dir_root);
			File[] file_list = DirPath.listFiles();
			read_file_content(file_list);
			return file_list;
		}
		
		//讀取路徑加過濾器
		public static File[] check_dir(String dir_root,FilenameFilter filter){
			File DirPath = new File(Environment.getExternalStorageDirectory()+dir_root);
			File[] file_list = DirPath.listFiles(filter);
			read_file_content(file_list);
			return file_list;
		}

}